import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    students: [
      { id: 1, name: '张三', studentId: '20230001', gender: '男', class: '计算机1班' },
      { id: 2, name: '李四', studentId: '20230002', gender: '女', class: '软件工程1班' },
      { id: 3, name: '王五', studentId: '20230003', gender: '男', class: '网络工程1班' },
      { id: 4, name: '赵六', studentId: '20230004', gender: '女', class: '软件工程2班' },
      { id: 5, name: '孙七', studentId: '20230005', gender: '男', class: '计算机2班' },
      { id: 6, name: '周八', studentId: '20230006', gender: '女', class: '人工智能1班' },
      { id: 7, name: '吴九', studentId: '20230007', gender: '男', class: '信息安全1班' },
      { id: 8, name: '郑十', studentId: '20230008', gender: '女', class: '大数据1班' }
    ],
    dormitories: [
      { id: 101, roomNumber: '101', floor: 1, bedCount: 4, availableBeds: 2 },
      { id: 102, roomNumber: '102', floor: 1, bedCount: 4, availableBeds: 0 },
      { id: 103, roomNumber: '103', floor: 1, bedCount: 4, availableBeds: 3 },
      { id: 104, roomNumber: '104', floor: 1, bedCount: 4, availableBeds: 1 },
      { id: 201, roomNumber: '201', floor: 2, bedCount: 4, availableBeds: 4 },
      { id: 202, roomNumber: '202', floor: 2, bedCount: 4, availableBeds: 2 },
      { id: 203, roomNumber: '203', floor: 2, bedCount: 4, availableBeds: 0 },
      { id: 204, roomNumber: '204', floor: 2, bedCount: 4, availableBeds: 3 },
      { id: 301, roomNumber: '301', floor: 3, bedCount: 4, availableBeds: 4 },
      { id: 302, roomNumber: '302', floor: 3, bedCount: 4, availableBeds: 1 }
    ]
  },
  mutations: {
    addStudent(state, student) {
      state.students.push(student)
    },
    updateStudent(state, updatedStudent) {
      const index = state.students.findIndex(s => s.id === updatedStudent.id)
      if (index !== -1) {
        state.students.splice(index, 1, updatedStudent)
      }
    },
    deleteStudent(state, id) {
      state.students = state.students.filter(s => s.id !== id)
    },

    addDormitory(state, dormitory) {
      state.dormitories.push(dormitory)
    },
    updateDormitory(state, updatedDormitory) {
      const index = state.dormitories.findIndex(d => d.id === updatedDormitory.id)
      if (index !== -1) {
        state.dormitories.splice(index, 1, updatedDormitory)
      }
    },
    deleteDormitory(state, id) {
      state.dormitories = state.dormitories.filter(d => d.id !== id)
    }
  },
  actions: {
    addStudent({ commit }, student) {
      commit('addStudent', student)
    },
    updateStudent({ commit }, student) {
      commit('updateStudent', student)
    },
    deleteStudent({ commit }, id) {
      commit('deleteStudent', id)
    },

    addDormitory({ commit }, dormitory) {
      commit('addDormitory', dormitory)
    },
    updateDormitory({ commit }, dormitory) {
      commit('updateDormitory', dormitory)
    },
    deleteDormitory({ commit }, id) {
      commit('deleteDormitory', id)
    }
  },
  getters: {
    students: state => state.students,
    dormitories: state => state.dormitories
  }
})